<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="员工姓名" prop="staffId">
                <el-input
                    v-model="queryParams.staffId"
                    placeholder="请输入员工姓名"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <TableCard>
            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="hrmContractList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column
                    label="合同文件"
                    align="center"
                    prop="fileUrl"
                    width="150"
                >
                    <template slot-scope="scope">
                        <el-link
                            :href="baseUrl + scope.row.fileUrl"
                            :underline="false"
                            target="_blank"
                        >
                            <img
                                src="@/assets/images/am/filext/DOC.png"
                                style="width: 50px"
                            />
                        </el-link>
                    </template>
                </el-table-column>
                <el-table-column
                    label="员工姓名"
                    align="center"
                    prop="staffRealName"
                />
                <el-table-column label="公司名" align="center" prop="company" />
                <el-table-column
                    label="公司纳税识别号"
                    align="center"
                    prop="companyNum"
                />
                <el-table-column
                    label="工资"
                    align="center"
                    prop="salary"
                    width="160"
                />
                <el-table-column
                    label="合同开始日期"
                    align="center"
                    prop="startDate"
                    width="160"
                >
                    <template slot-scope="scope">
                        <span>{{
                            parseTime(scope.row.startDate, "{y}-{m}-{d}")
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="合同结束日期"
                    align="center"
                    prop="endDate"
                    width="160"
                >
                    <template slot-scope="scope">
                        <span>{{
                            parseTime(scope.row.endDate, "{y}-{m}-{d}")
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="签订日期"
                    align="center"
                    prop="signDate"
                    width="160"
                >
                    <template slot-scope="scope">
                        <span>{{
                            parseTime(scope.row.signDate, "{y}-{m}-{d}")
                        }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="状态"
                    align="center"
                    prop="remark"
                    width="120"
                >
                    <template slot-scope="scope">
                        <span
                            v-if="compareDate(scope.row.endDate) == 2"
                            style="color: #ff4949; font-weight: 900"
                        >
                            已经超期
                        </span>
                        <span
                            v-if="compareDate(scope.row.endDate) == 1"
                            style="color: #f8ac59; font-weight: 900"
                        >
                            七天后超期
                        </span>
                        <span
                            v-if="compareDate(scope.row.endDate) == 0"
                            style="color: #1ab394; font-weight: 900"
                        >
                            正常
                        </span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                    width="150"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleAdd()"
                            v-hasPermi="['hrm:hrmContract:edit']"
                        >
                            续签
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>

        <!-- 添加或修改合同签订对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="800px"
            append-to-body
        >
            <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="120px"
            >
                <el-form-item label="合同文件地址" prop="fileUrl">
                    <file-upload v-model="form.fileUrl" :limit="1" />
                </el-form-item>
                <el-form-item label="公司名" prop="company">
                    <el-input
                        v-model="form.company"
                        placeholder="请输入公司名"
                        disabled
                    />
                </el-form-item>
                <el-form-item label="公司纳税识别号" prop="companyNum">
                    <el-input
                        v-model="form.companyNum"
                        placeholder="请输入公司纳税识别号"
                        disabled
                    />
                </el-form-item>
                <el-form-item label="员工" prop="staffId">
                    <el-input
                        style="width: 300px; height: 50px; width: 100%"
                        v-model="form.staffRealName"
                        placeholder="请选择员工"
                        readonly
                    >
                        <template #suffix>
                            <a
                                type="primary"
                                style="
                                    height: 45px;
                                    color: #00afff;
                                    padding: 0px 10px;
                                "
                                @click="openSecelctOneUser"
                                >选择</a
                            >
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item label="合同开始日期" prop="startDate">
                    <el-date-picker
                        clearable
                        v-model="form.startDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择合同开始日期"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="合同结束日期" prop="endDate">
                    <el-date-picker
                        clearable
                        v-model="form.endDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择合同结束日期"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="工资" prop="salary">
                    <el-input v-model="form.salary" placeholder="请输入工资" />
                </el-form-item>
                <el-form-item label="签订日期" prop="signDate">
                    <el-date-picker
                        clearable
                        v-model="form.signDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择签订日期"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input
                        v-model="form.remark"
                        type="textarea"
                        :rows="5"
                        placeholder="请输入内容"
                    />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 添加或修改合同签订对话框 -->
        <el-dialog
            :visible.sync="showSelectOneStaff"
            title="选择员工"
            width="1200px"
            append-to-body
        >
            <SelectOneStaff
                @selectedOneStaff="selectedOneStaff"
                @closeSelectOneStaff="closeSelectOneStaff"
            ></SelectOneStaff>
        </el-dialog>
    </div>
</template>

<script>
import {
    listHrmContract,
    getHrmContract,
    delHrmContract,
    addHrmContract,
    updateHrmContract,
} from "@/api/hrm/hrmContract";
import { getUser } from "@/api/system/user";
import SelectOneStaff from "@/views/hrm/common/selectOneStaff";
import { getMyCompanyTenant } from "@/api/common/companyTenant";

export default {
    name: "HrmContract",
    components: { SelectOneStaff },
    data() {
        return {
            baseUrl: process.env.VUE_APP_BASE_API,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 合同签订表格数据
            hrmContractList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            company: "",
            companyNum: "",
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                orderByColumn: "endDate",
                isAsc: "ascending",
                staffId: null,
                startDate: null,
                endDate: null,
                company: null,
                companyNum: null,
                salary: null,
                signDate: null,
                tenantId: null,
                fileUrl: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                staffId: [
                    {
                        required: true,
                        message: "员工id不能为空",
                        trigger: "blur",
                    },
                ],
                startDate: [
                    {
                        required: true,
                        message: "合同开始日期不能为空",
                        trigger: "blur",
                    },
                ],
                endDate: [
                    {
                        required: true,
                        message: "合同结束日期不能为空",
                        trigger: "blur",
                    },
                ],
                company: [
                    {
                        required: true,
                        message: "公司名不能为空",
                        trigger: "blur",
                    },
                ],
                companyNum: [
                    {
                        required: true,
                        message: "公司纳税识别号不能为空",
                        trigger: "blur",
                    },
                ],
                salary: [
                    {
                        required: true,
                        message: "工资不能为空",
                        trigger: "blur",
                    },
                ],
                signDate: [
                    {
                        required: true,
                        message: "签订日期不能为空",
                        trigger: "blur",
                    },
                ],
                fileUrl: [
                    {
                        required: true,
                        message: "合同文件地址不能为空",
                        trigger: "blur",
                    },
                ],
            },
            showSelectOneStaff: false,
        };
    },
    created() {
        this.getCompany();
        this.getList();
    },
    methods: {
        /** 查询合同签订列表 */
        getList() {
            this.loading = true;
            listHrmContract(this.queryParams).then((response) => {
                this.hrmContractList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        getCompany() {
            getMyCompanyTenant().then((res) => {
                let com = res.data;
                console.log(com);
                this.company = com.name;
                this.companyNum = com.idNum;
                console.log(this.form);
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                staffId: null,
                startDate: null,
                endDate: null,
                salary: null,
                signDate: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null,
                tenantId: null,
                fileUrl: null,
                company: this.company,
                companyNum: this.companyNum,
            };
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加合同签订";
            console.log(this.form);
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateHrmContract(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addHrmContract(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },

        openSecelctOneUser() {
            this.showSelectOneStaff = true;
            this.$refs["form"].clearValidate();
        },
        //选择完一条用户
        selectedOneStaff(staff) {
            console.log("选择一条用户：" + JSON.stringify(staff));
            this.form.staffId = staff.id;
            this.form.staffRealName = staff.realName;
            this.showSelectOneStaff = false;
        },
        // 关闭选择一个用户弹窗
        closeSelectOneStaff() {
            this.showSelectOneStaff = false;
        },

        compareDate(endDate) {
            if (endDate) {
                let end = endDate.replace("-", "/");
                end = new Date(Date.parse(end));
                //已经超期
                if (new Date() > end) {
                    return 2;
                } else {
                    let start = new Date(new Date().setDate(end.getDate() - 7));
                    if (new Date() > start) {
                        return 1;
                    } else {
                        return 0;
                    }
                }
            } else {
                return 0;
            }
        },
    },
};
</script>
